from django.contrib import messages
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.views.decorators.http import require_http_methods
from server.apps.main.logic.posts import queries as task_queries
from server.apps.main.logic.posts import commands
from server.apps.main.logic.usecases import create_new_task, edit_task

class HttpResponseTemporaryRedirect(HttpResponseRedirect):
    status_code = 307

@require_http_methods(['GET'])
def index(request: HttpRequest) -> HttpResponse:
    """
    Main (or index) view.

    Returns rendered default page to the user.
    Typed with the help of ``django-stubs`` project.
    """
    
    return render(request, 'main/index.html')

@require_http_methods(['GET','POST'])
def add(request: HttpRequest) -> HttpResponse:
    if request.method == 'POST':
        is_valid, title = create_new_task.run(
        title=request.POST.get('title'),
        description=request.POST.get('description'),
        is_done=bool(request.POST.get('is_done')),
        )
        if is_valid:
            messages.info(request, 'Task {0} created.'.format(title))
        else:
            messages.info(request, 'Task not created. Fields cannot be empty')
        return render(request, 'main/add.html')
    else:
        return render(request, 'main/add.html')

@require_http_methods(['GET','POST'])
def show(request: HttpRequest) -> HttpResponse:
    tasks = task_queries.list_all()
    return render(request, 'main/show.html', {'tasks': tasks})

@require_http_methods(['POST'])
def del_task(request: HttpRequest) -> HttpResponse:
    commands.del_task(request.POST.get('id'))
    return HttpResponseTemporaryRedirect(request.headers["Referer"])

@require_http_methods(['POST'])
def done_task(request: HttpRequest) -> HttpResponse:
    commands.done_task(request.POST.get('id'))
    return HttpResponseTemporaryRedirect(request.headers["Referer"])


def search(request: HttpRequest) -> HttpResponse:
    if request.method == 'POST':
        if request.POST.get('search') == '':
            messages.info(request, 'Input search text.')
            return render(request, 'main/search.html')
        else:
            tasks = task_queries.search(request.POST.get('search'))
            if not tasks:
                messages.info(request, 'Task not found.')
            return render(request, 'main/search.html', {'tasks': tasks, 'text': request.POST.get('search')})
    else:
        return render(request, 'main/search.html')
    
def edit(request: HttpRequest) -> HttpResponse:
    if request.method == 'POST':
        if request.POST.get('write'):
            is_changed, is_valid, id = edit_task.run(
            id=request.POST.get('id'),
            title=request.POST.get('title'),
            description=request.POST.get('description'),
            is_done=bool(request.POST.get('is_done')),
            )
            if is_valid and is_changed:
                task = task_queries.show_one(request.POST.get('id'))
                messages.info(request, 'Task changed.')
                return render(request, 'main/edit.html', {'task': task})
            elif (not is_valid):
                task = task_queries.show_one(request.POST.get('id'))
                messages.info(request, 'Fields cannot be empty')
                return render(request, 'main/edit.html', {'task': task})
            elif not is_changed:
                messages.info(request, 'Task {0} not found'.format(id))
                return render(request, 'main/edit.html')
        else:
            task = task_queries.show_one(request.POST.get('id'))
            return render(request, 'main/edit.html', {'task': task})
    else:
        return render(request, 'main/edit.html')
